package com.chatroom.controller;

import com.chatroom.enity.Result;
import com.chatroom.enity.User;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author 计算机系 ITAEM 纱雾休比我老婆
 * @Description
 * @date 2022/8/9 11:43
 */
@RestController
@RequestMapping("/chat")
public class UserController {

    @PostMapping("/login")
    public Result login(@RequestBody User user, HttpSession session){
        Result result = new Result();
        if(user != null && "123".equals(user.getPassword())){
            result.setFlag(true);
            session.setAttribute("user",user.getUsername());
        } else {
            result.setFlag(false);
            result.setMessage("登录失败");
        }
        return result;
    }

    /**
     * 登录成功后跳转到聊天页面.
     */
    @GetMapping("/toChatroom")
    public String toChatroom() {
        return "chat";
    }

    @GetMapping(value = "/getUsername",produces = "text/plain;charset=utf-8")
    @ResponseBody
    public String getUsername(HttpSession session){
        String username = (String) session.getAttribute("user");
        return username;
    }
}
